const { ccclass, property } = cc._decorator;

@ccclass
export default class AudioMgr extends cc.Component {
  private _bgmVolume: number = 1.0;
  private _sfxVolume: number = 1.0;
  private _bgmAudioID: number = -1;

  public constructor() {
    super();
    this.init();
  }

  getBmgVolume() {
    return this._bgmVolume;
  }

  getSfxVolume() {
    return this._sfxVolume;
  }

  /**
   * 初始化， 音量，监听 hide,show事件控制音声
   *
   * @memberof AudioMgr
   */
  init() {
    let v = cc.sys.localStorage.getItem("ly_bgmVolume");
    if (v != null) {
      this._bgmVolume = parseFloat(v);
    }

    let t = cc.sys.localStorage.getItem("ly_sfxVolume");
    if (t != null) {
      this._sfxVolume = parseFloat(t);
    }
  }

  /**
   *
   *
   * @param {string} name
   * @param {string} subGamePath
   * @returns
   * @memberof AudioMgr
   */
  getUrl(name: string, isCommon?: boolean) {
    // if (window.subGameDir && !isCommon) {
    //   return `/${window.subGameDir}/audio/${name}`;
    // }
    if (isCommon) {
      return `/commonUI/commonAudio/${name}`;
    }
    return `/audio/${name}`;
  }

  stopBGM() {
    if (this._bgmAudioID >= 0) {
      cc.audioEngine.stop(this._bgmAudioID);
      this._bgmAudioID = -1;
    }
  }

  /**
   * 播放背景音乐
   *
   * @param {string} name
   * @memberof AudioMgr
   */
  playBGM(name: string, isLoop: boolean = true) {
    this.stopBGM();
    let loop = isLoop ? isLoop : true;
    cc.loader.loadRes(this.getUrl(name), cc.AudioClip, (err, audioClip) => {
      if (err) {
        console.error(err);
        return;
      }
      this._bgmAudioID = cc.audioEngine.play(
        audioClip,
        isLoop,
        this._bgmVolume
      );
    });
  }

  /**
   * 设置背景音乐
   *
   * @param {number} v
   * @param {boolean} force
   * @memberof AudioMgr
   */
  setBGMVolume(v: number, force?: boolean) {
    if (this._bgmAudioID >= 0) {
      if (v > 0) {
        cc.audioEngine.resume(this._bgmAudioID);
      } else {
        cc.audioEngine.pause(this._bgmAudioID);
      }
    }
    if (this._bgmVolume != v || force) {
      cc.sys.localStorage.setItem("ly_bgmVolume", v);
      this._bgmVolume = v;
      cc.audioEngine.setVolume(this._bgmAudioID, v);
    }
  }

  /**
   * 播放音效
   *
   * @param {string} name
   * @memberof AudioMgr
   */
  playSFX(name: string, isCommon?: boolean) {
    let audioUrl = this.getUrl(name, isCommon);
    if (this._sfxVolume > 0) {
      cc.loader.loadRes(audioUrl, cc.AudioClip, (err, audioClip) => {
        if (err) {
          console.error(err);
          return;
        }
        cc.audioEngine.play(audioClip, false, this._sfxVolume);
      });
    }
  }

  /**
   * 设置音效音量
   *
   * @param {number} v
   * @memberof AudioMgr
   */
  setSFXVolume(v: number) {
    if (this._sfxVolume != v) {
      cc.sys.localStorage.setItem("ly_sfxVolume", v);
      this._sfxVolume = v;
    }
  }

  /**
   * 全部暂停
   *
   * @memberof AudioMgr
   */
  pauseAll() {
    cc.audioEngine.pauseAll();
  }

  /**
   * 全部继续
   *
   * @memberof AudioMgr
   */
  resumeAll() {
    cc.audioEngine.resumeAll();
  }
}
